//
//  SPESMARTUNITTESTViewController.m
//  SpeSmartUnitTester
//
//  Created by hfqf123 on 08/09/2020.
//  Copyright (c) 2020 hfqf123. All rights reserved.
//

#import "SPESMARTUNITTESTViewController.h"
#import "HYXRootViewController.h"
#import "HYXLginViewController.h"
#import "HYXUnitTestNode.h"
#import <coobjc/coobjc.h>
#import <cocore/cocore.h>
@interface SPESMARTUNITTESTViewController ()

@end

@implementation SPESMARTUNITTESTViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    
    UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn1 addTarget:self action:@selector(btn1Clicked) forControlEvents:UIControlEventTouchUpInside];
    [btn1 setBackgroundColor:[UIColor systemBlueColor]];
    [btn1 setFrame:CGRectMake(30, 200, [UIScreen mainScreen].bounds.size.width-60, 40)];
    [btn1 setTitle:@"开始流程1自动单元测试" forState:UIControlStateNormal];
    [btn1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.view addSubview:btn1];
    
    UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn2 setBackgroundColor:[UIColor systemBlueColor]];
    [btn2 setFrame:CGRectMake(30, 290, [UIScreen mainScreen].bounds.size.width-60, 40)];
    [btn2 setTitle:@"开始流程2自动单元测试" forState:UIControlStateNormal];
    [btn2 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.view addSubview:btn2];
}

- (void)btn1Clicked{
//    [self testCoobjc1];
    [HYXHttpPushExtensionFactory factory].
    setPath(@"http://127.0.0.1:18080/unittest/autotest/path1")
    .setNeedToast(NO)
    .setResponseBlock(^(id<HYXHttpResponseModel> resp) {
        HYXHttpResonseModule *ret = (HYXHttpResonseModule *)resp;
        HYXUnitTestNode *root = [HYXUnitTestNode yy_modelWithJSON:ret.result];
        [root rebuildParentNode];
        if(ret.state.integerValue == 0){
            [[HYXUnitTestNode getNextNode:root]excuteNode];
        }else{

        }

    }).setErrorBock(^(id<HYXHttpFailModel> error) {

    }).post();

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



- (void)testCoobjc1{
    co_launch(^{
        NSArray*arr = batch_await(@[[self fun1],[self fun2]]);
        NSLog(@"1");
    });
}

#pragma mark - coobjc
- (COPromise *)fun1{
    COPromise *promise = [COPromise promise];
    [HYXHttpPushExtensionFactory factory].
    setPath(@"http://127.0.0.1:18080/unittest/autotest/path2")
    .setNeedToast(NO)
    .setResponseBlock(^(id<HYXHttpResponseModel> resp) {
        HYXHttpResonseModule *ret = (HYXHttpResonseModule *)resp;
        HYXUnitTestNode *root = [HYXUnitTestNode yy_modelWithJSON:ret.result];
        [promise fulfill:root];
    }).setErrorBock(^(id<HYXHttpFailModel> error) {
        [promise fulfill:nil];
    }).post();
    return promise;
}

- (COPromise *)fun2{
    COPromise *promise = [COPromise promise];
    [HYXHttpPushExtensionFactory factory].
    setPath(@"http://127.0.0.1:18080/unittest/autotest/path1")
    .setNeedToast(NO)
    .setResponseBlock(^(id<HYXHttpResponseModel> resp) {
        HYXHttpResonseModule *ret = (HYXHttpResonseModule *)resp;
        HYXUnitTestNode *root = [HYXUnitTestNode yy_modelWithJSON:ret.result];
        [promise fulfill:root];
    }).setErrorBock(^(id<HYXHttpFailModel> error) {
        [promise fulfill:nil];
    }).post();
    return promise;
}
@end
